package com.exahq.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exahq.entity.Partner;
import com.exahq.entity.SelectData;
import java.util.List;
import com.exahq.entity.Userinfo;
import com.exahq.common.lang.Result;
import com.exahq.service.UserinfoService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhq
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/userinfo")
public class UserinfoController {
    @Autowired
    UserinfoService userinfoService;

    /***
     * 增加
     * @param userInfo
     * @return
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @PostMapping("save")
    public Result saveEdit(@RequestBody Userinfo userInfo){
        if (userInfo!=null && !userInfo.equals("")){
                boolean b = userinfoService.saveOrUpdate(userInfo);
                if (b){
                    QueryWrapper<Userinfo> userinfoQueryWrapper =new QueryWrapper<Userinfo>();
                    userinfoQueryWrapper.eq("del_flag",1);
                    userinfoQueryWrapper.eq("uid",userInfo.getUid());
                    Userinfo userinfo =userinfoService.getOne(userinfoQueryWrapper);
                    return Result.succ(userinfo);
                }
        }
        return Result.succ(null);
    }

    @RequestMapping(value = "getOne",method = RequestMethod.POST)
    @PostMapping("getOne")
    public Result getOne(@RequestBody String uidInfo){
        JSONObject json_test = JSONObject.fromObject(uidInfo);
        System.out.println(json_test.get("uidInfo").toString());
       Userinfo userinfo =userinfoService.getById(Integer.valueOf(json_test.get("uidInfo").toString()));
        return Result.succ(userinfo);
    }

    /**
     *
     * @param query
     * @param response
     * @return
     */
    @PostMapping("/users")
    public Result company(@RequestBody Map<String,String> query, HttpServletResponse response) {
        SelectData selectData =new SelectData();
        Integer currentPage = Integer.valueOf(query.get("pageIndex"));
        selectData.setPageIndex(currentPage);
        if(currentPage == null || currentPage < 1) selectData.setPageIndex(0);
        selectData.setName(query.get("name"));
        selectData.setAddress(query.get("region"));
        selectData.setSex(query.get("sex").trim());
        selectData.setPageSize(Integer.valueOf(query.get("pageSize")));
        List<Userinfo> userinfoList =userinfoService.getByQuery(selectData);
        int totalCount = userinfoService.getCount(selectData);
        Map<Object,Object> map =new HashMap<Object,Object>();
        map.put("userinfoList",userinfoList);
        map.put("totalCount",totalCount);
        return Result.succ(map);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result detail(@PathVariable(name = "id") Integer id) {
        Userinfo userinfo = userinfoService.getById(id);
        return Result.succ(userinfo);
    }
    /**
     * 修改/新增
     * @param userinfo
     * @return
     */
    @PostMapping("saveEdit")
    public Result save(@RequestBody Userinfo userinfo) {
        if (userinfo!=null && !userinfo.equals("")){
            System.out.println(userinfo);
            if (userinfo.getUidInfo() == null){
                boolean b = userinfoService.save(userinfo);
                if (b){
                    List<Userinfo> userinfoList =userinfoService.list();
                    return Result.succ("新添成功",userinfoList);
                }
            }else {
                if (userinfoService.saveOrUpdate(userinfo))
                    return Result.succ("修改成功",userinfoService.saveOrUpdate(userinfo));
            }
        }
        return Result.succ(null);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    public Result del(@PathVariable(name = "id") Integer id) {
        return Result.succ( userinfoService.delete(id));
    }
}
